package com.yingxin.yxerp.module.permission.vo.req.helper;


import cn.hutool.core.collection.CollectionUtil;
import com.yingxin.yx.framework.commons.helper.ListHelper;
import com.yingxin.yx.framework.user.dto.AppPermissionDto;
import com.yingxin.yxerp.module.permission.entity.AppPermission;
import com.yingxin.yxerp.module.permission.entity.AppRolePermission;
import com.yingxin.yxerp.module.permission.entity.constant.PermissionConst;

import java.util.*;

/**
 * 菜单，权限树结构工具类
 */
public class UserAppPermissionTreeHelper {

    /**
     * 构建树
     *
     * @param permissions
     * @return
     */
    public static List<AppPermissionDto> build(List<AppPermissionDto> permissions) {
        List<AppPermissionDto> tree = new ArrayList<>();
        for (AppPermissionDto permission : permissions) {
            if (PermissionConst.ROOT_ID.equals(permission.getParentId())) {
                findChild(permission, permissions);
                if (CollectionUtil.isEmpty(permission.getChildren())) {
                    continue;
                }
                tree.add(permission);
            }
        }
        CollectionUtil.sort(tree, Comparator.comparingInt(AppPermissionDto::getSort));
        return tree;
    }

    /**
     * 递归查找子级
     *
     * @param parent
     * @param permissions
     */
    private static void findChild(AppPermissionDto parent, List<AppPermissionDto> permissions) {
        for (AppPermissionDto permission : permissions) {
            if (null != permission.getParentId() && permission.getParentId().equals(parent.getId())) {
                findChild(permission, permissions);
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(permission);
            }
        }
    }


    /**
     * @param AppRolePermissionMap
     * @param permissions
     * @return
     */
    public static Collection<AppPermission> buildParent(Map<String, AppRolePermission> AppRolePermissionMap, List<AppPermission> permissions) {
        Map<String, AppPermission> parentMap = new HashMap<>();
        Map<String, AppPermission> permissionMap = ListHelper.toMap(AppPermission::getId, permissions);
        for (AppRolePermission rp : AppRolePermissionMap.values()) {
            AppPermission children = permissionMap.get(rp.getPermissionId());
            findParent(children, permissionMap, parentMap);
        }
        return parentMap.values();
    }


    /**
     * @param children
     * @param permissionMap
     * @param parentMap
     */
    private static void findParent(AppPermission children, Map<String, AppPermission> permissionMap, Map<String, AppPermission> parentMap) {
        AppPermission parent = parentMap.get(children.getParentId());
        if (parent == null) {
            AppPermission permission = permissionMap.get(children.getParentId());
            if (permission != null) {
                parentMap.put(permission.getId(), permission);
                findParent(permission, permissionMap, parentMap);
            }
        }
    }


    /**
     * 构建树
     *
     * @param permissions
     * @return
     */
    public static List<AppPermissionDto> buildMenu(List<AppPermissionDto> permissions) {
        List<AppPermissionDto> tree = new ArrayList<>();
        for (AppPermissionDto permission : permissions) {
            if (!PermissionConst.Type.MENU.equals(permission.getType())) {
                continue;
            }
            if (PermissionConst.ROOT_ID.equals(permission.getParentId())) {
                findMenuChild(permission, permissions);
                if (CollectionUtil.isEmpty(permission.getChildren())) {
                    continue;
                }
                tree.add(permission);
            }
        }
        treeSoft(tree);

        // CollectionUtil.sort(tree, Comparator.comparingInt(AppPermissionDto::getSort));

        return tree;
    }

    private static void treeSoft(List<AppPermissionDto> tree){
        CollectionUtil.sort(tree, Comparator.comparingInt(AppPermissionDto::getSort));
        for (AppPermissionDto permission : tree) {
            if (CollectionUtil.isEmpty(permission.getChildren())) {
                continue;
            }
            treeSoft(permission.getChildren());
        }
    }

    /**
     * 递归查找子级
     *
     * @param parent
     * @param permissions
     */
    private static void findMenuChild(AppPermissionDto parent, List<AppPermissionDto> permissions) {
        for (AppPermissionDto permission : permissions) {
            if (!PermissionConst.Type.MENU.equals(permission.getType())) {
                continue;
            }
            if (null != permission.getParentId() && permission.getParentId().equals(parent.getId())) {
                findMenuChild(permission, permissions);
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(permission);
            }
        }
    }


}